package com.pm.controller.supervision;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.security.User;
import com.pm.entity.po.supervision.FrontLineSupervision;
import com.pm.entity.vo.supervision.FrontLineSupervisionVo;
import com.pm.service.supervision.SupervisionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: pm_info_api
 * @description: 一级督导
 * @author: xzj
 * @create: 2020-08-26 11:44
 **/

@Api(tags = "一级督导核验")
@UserLoginToken
@RestController
@RequestMapping("/supervision")
public class SupervisionController {

    @Autowired
    SupervisionService supervisionService;
    @ApiOperation("添加一级督导核验")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value="添加一级督导核验")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody FrontLineSupervision frontLineSupervision, @CurrentUser User user){
        Response<Integer> response = null;
        // 添加人
        frontLineSupervision.setAddUser(user.getUserId());
        // 修改人
        frontLineSupervision.setEditUser(user.getUserId());

        Integer result =  supervisionService.addSupervision(frontLineSupervision);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询部门信息")
    public Response<PageInfo<FrontLineSupervision>> getList(FrontLineSupervision frontLineSupervision, Pages page) {
        PageInfo<FrontLineSupervision> result = supervisionService.getListByPage(frontLineSupervision, page);
        Response<PageInfo<FrontLineSupervision>> res = new Response<PageInfo<FrontLineSupervision>>(ExpCodeState.query_data_success, result);
        return res;
    }
    /**
     * 根据id查部门
     * @return
     */
    @ApiOperation("Id查一级督导核验")
    @PostMapping("/getSupervisionId")
    @ResponseBody
    public Response<List<FrontLineSupervisionVo>> load(FrontLineSupervision frontLineSupervision){
        List<FrontLineSupervisionVo> frontLineSupervisionOne = supervisionService.loadOne(frontLineSupervision);
        Response<List<FrontLineSupervisionVo>> response= new Response<>(ExpCodeState.query_data_success,frontLineSupervisionOne);
        return response;
    }

    /**
     * 删除
     */
    @ApiOperation("删除一级督导核验")
    @PostMapping("/delete")
    @ResponseBody
    @MyLog(value="删除一级督导核验")
    public Response<Integer> delete(@RequestParam("id") Integer id){
        boolean res = supervisionService.delete(id);
        if(res){
            return new Response<>(ExpCodeState.remove_data_success);
        }else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }


    @ApiOperation("修改一级督导核验")
    @PostMapping("/updateAward")
    @ResponseBody
    @MyLog(value="修改一级督导核验")
    public Response update(@RequestBody FrontLineSupervision frontLineSupervision,@CurrentUser User user) {
        frontLineSupervision.setEditUser(user.getUserId());
        boolean res = supervisionService.updatesupervision(frontLineSupervision);
        if(res){
            return new Response<>(ExpCodeState.update_data_success);
        }else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }
}
